package com.cindata

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import grails.converters.JSON
import java.text.SimpleDateFormat
import org.grails.web.util.WebUtils

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class MortgageCategoryItemController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) 
    {
        params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
        params.offset = params.offset ? params.offset.toInteger() : 0
        def sort = params['sort']
        def order = params['order']
        def name = params['name']
        def sql = " from MortgageCategoryItem o where 1=1  "
        if (name)
        {
            sql += " and o.name like '%${name}%'"
        }
        def countSql = " select count(o.id) " + sql
        if (order && sort)
        {
            sql += " order by o.${sort} ${order}"
        }
        else
        {
            sql += "order by o.name desc "
        }
        def mortgageCategoryItemList = MortgageCategoryItem.executeQuery(sql, [max: params.max, offset: params.offset])
        def mortgageCategoryItemCount = MortgageCategoryItem.executeQuery(countSql)
        respond mortgageCategoryItemList, model: [mortgageCategoryItemCount: mortgageCategoryItemCount[0], params: params]
    }

    def show(MortgageCategoryItem mortgageCategoryItem) {
        respond mortgageCategoryItem
    }

    def create() {
        respond new MortgageCategoryItem(params)
    }

    @Transactional
    def save(MortgageCategoryItem mortgageCategoryItem) {
        if (mortgageCategoryItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCategoryItem.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCategoryItem.errors, view:'create'
            return
        }

        mortgageCategoryItem.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'mortgageCategoryItem.label', default: 'MortgageCategoryItem'), mortgageCategoryItem.id])
                redirect mortgageCategoryItem
            }
            '*' { respond mortgageCategoryItem, [status: CREATED] }
        }
    }

    def edit(MortgageCategoryItem mortgageCategoryItem) {
        respond mortgageCategoryItem
    }

    @Transactional
    def update(MortgageCategoryItem mortgageCategoryItem) {
        if (mortgageCategoryItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (mortgageCategoryItem.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond mortgageCategoryItem.errors, view:'edit'
            return
        }

        mortgageCategoryItem.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'mortgageCategoryItem.label', default: 'MortgageCategoryItem'), mortgageCategoryItem.id])
                redirect mortgageCategoryItem
            }
            '*'{ respond mortgageCategoryItem, [status: OK] }
        }
    }

    @Transactional
    def delete(MortgageCategoryItem mortgageCategoryItem) {

        if (mortgageCategoryItem == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        mortgageCategoryItem.delete flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'mortgageCategoryItem.label', default: 'MortgageCategoryItem'), mortgageCategoryItem.id])
                redirect action:"index", method:"GET"
            }
            '*'{ render status: NO_CONTENT }
        }
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'mortgageCategoryItem.label', default: 'MortgageCategoryItem'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
